﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.DB;
using Sector7.Medical.DAO.Standard;
using Sector7.Framework.Utility;
using Sector7.Medical.EntityType;

namespace Sector7.Medical.ADDM.Controls
{
    /// <summary>
    /// InterviewSettingControl.xaml 的交互逻辑
    /// </summary>
    public partial class InterviewSettingControl : UserControl
    {
        public InterviewSettingControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            var interviewDate = DateTime.Now;
            lblInterviewDate.Content = interviewDate.ToString("yyyy年MM月dd日");
            lblInterviewDate.Tag = interviewDate;

            var templateList = DAOFactory.GetDAO<InterviewTemplateDAO>().ReadAll();
            ClientUtil.LoadItemsSource(cbbTemplateList, templateList);
        }

        private void dtgExaminee_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(string)))
            {
                string data = e.Data.GetData(typeof(string)) as string;

                var examineeList = new List<T7_Examinee>();

                // 4\t沈佳宜\t510922199001010000\r\n5\t柯景腾\t510922199001010000\r\n
                var examineeStrArray = data.Replace("\r\n", "#").Split('#');
                foreach (var examineeStr in examineeStrArray)
                {
                    if (examineeStr != null && examineeStr != "")
                    {
                        var colStrArray = examineeStr.Replace("\t", "#").Split('#');
                        var examinee = new T7_Examinee();
                        examinee.EmpId = Convert.ToInt32(colStrArray[0]);
                        examinee.Fullname = colStrArray[1];
                        examinee.IdentityId = colStrArray[2];

                        examineeList.Add(examinee);
                    }
                }

                ClientUtil.LoadItemsSource(dtgExaminee, examineeList);

                gpbExaminee.Header = string.Format("考官列表 [共 {0} 人]",
                    examineeList.Count);
            }
        }

        private void dtgExaminer_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(string)))
            {
                string data = e.Data.GetData(typeof(string)) as string;

                var examinerList = new List<T7_Examiner>();

                // 4\t沈佳宜\t510922199001010000\t98\r\n5\t柯景腾\t510922199001010000\t75\r\n
                var examinerStrArray = data.Replace("\r\n", "#").Split('#');
                foreach (var examinerStr in examinerStrArray)
                {
                    if (examinerStr != null && examinerStr != "")
                    {
                        var colStrArray = examinerStr.Replace("\t", "#").Split('#');
                        var examiner = new T7_Examiner();
                        examiner.EmpId = Convert.ToInt32(colStrArray[0]);
                        examiner.Fullname = colStrArray[1];
                        examiner.IdentityId = colStrArray[2];
                        examiner.ScoreOfWritten = Convert.ToDecimal(colStrArray[3]);

                        examinerList.Add(examiner);
                    }
                }

                ClientUtil.LoadItemsSource(dtgExaminer, examinerList);

                gpbExaminer.Header = string.Format("考生列表 [共 {0} 人]",
                    examinerList.Count);
            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            object result = null;
            var interview = new T7_Interview();

            interview.TemplateId = Convert.ToInt32(cbbTemplateList.SelectedValue);
            interview.ScrollInterval = Convert.ToInt32(txbScrollInterval.Text);
            interview.InterviewName = txbInterviewName.Text;
            interview.InterviewDate = (DateTime)lblInterviewDate.Tag;

            result = DAOFactory.GetDAO<InterviewDAO>().Create(interview);
            interview.InterviewId = Convert.ToInt32(result);

            var examineeList = dtgExaminee.ItemsSource as List<T7_Examinee>;
            examineeList.ForEach(delegate(T7_Examinee __examinee)
            {
                __examinee.InterviewId = interview.InterviewId;
            });
            result = DAOFactory.GetDAO<ExamineeDAO>().CreateAll(examineeList);

            var examinerList = dtgExaminer.ItemsSource as List<T7_Examiner>;
            examinerList.ForEach(delegate(T7_Examiner __examiner)
            {
                __examiner.InterviewId = interview.InterviewId;
            });
            result = DAOFactory.GetDAO<ExaminerDAO>().CreateAll(examinerList);
        }
    }
}
